package com.smart.stock.module.maindata.services;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.smart.stock.common.db.MyMongoTemplate;
import com.smart.stock.common.util.spring.EntityUtil;
import com.smart.stock.common.util.stock.StockSpider;
import com.smart.stock.common.util.stock.StockUtil;
import com.smart.stock.common.util.stock.impl.JisiluSpilder;
import com.smart.stock.module.maindata.entity.*;
import com.smart.stock.module.maindata.repository.StockHisDividendRepository;
import com.smart.stock.module.maindata.repository.StockRepository;
import com.smart.stock.module.maindata.vo.StockVo;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Stock service
 *
 * @author chenguoxiang
 * @create 2018-03-27 10:27
 **/
@Service
public class StockCollectService {
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StockCodeInfoService stockCodeInfoService;

    public void addCollect(String[] codes) {
        List<Stock> StockList = template.find(new Query(Criteria.where("_id").in(codes)), Stock.class);
        String entityName = EntityUtil.getEntityNameByCode(StockCollect.class, null);

        List<StockCollect> StockCollectList = new ArrayList<StockCollect>();
        for (int i = 0; i < StockList.size(); i++) {
            Stock stock = StockList.get(i);

            //用BeanUtils复制所有属性
            StockCollect stockCollect = new StockCollect();
            BeanUtils.copyProperties(stock,stockCollect);

            //设置股票名称name
            StockCodeInfo stockCodeInfo = stockCodeInfoService.getByCode(stock.getCode()).get(0);
            stockCollect.setName(stockCodeInfo.getName());

            StockCollectList.add(stockCollect);
        }
        template.insertAll(StockCollectList);
    }

    public void delcollect(String[] codes) {
        DeleteResult deleteResult = template.remove(new Query(Criteria.where("_id").in(codes)), StockCollect.class);
        System.out.println("StockCollect删除的记录数："+deleteResult.getDeletedCount());
    }

    public List<StockCollect> list() {
        Query query = new Query();
        List<StockCollect> StockList = template.find(query, StockCollect.class);
        return StockList;
    }


}
